package jay.home.bet.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import jay.home.bet.dao.OrderDao;
import jay.home.bet.domain.Item;
import jay.home.bet.domain.Order;

public class SqlMapOrderDao extends SqlMapClientDaoSupport implements OrderDao
{

	public Order getOrder(String id) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		return (Order)getSqlMapClientTemplate().queryForObject("getOrder", map);
	}

	public List<Order> getOrders(int page, int range) throws DataAccessException
	{
		return (List<Order>)getSqlMapClientTemplate().queryForList("getOrder", null, (page - 1) * range, range);
	}

	public List<Order> getOrdersByUser(String username, int page, int range) throws DataAccessException
	{
		Map map = new HashMap();
		map.put("username", username);
		map.put("alive", false);
		return (List<Order>)this.getSqlMapClientTemplate().queryForList("getOrder", map , (page - 1) * range, range);
	}

	public List<Order> getOrdersByComp(String compid, int page, int range) throws DataAccessException
	{
		Map map = new HashMap();
		map.put("compid", compid);
		map.put("alive", true);
		return (List<Order>)this.getSqlMapClientTemplate().queryForList("getOrdersByComp", map , (page - 1) * range, range);
	}

	public void insertOrder(Order order) throws DataAccessException
	{
		getSqlMapClientTemplate().insert("insertOrder", order);
		for(Item item : order.getItems())
			insertItem(item);
	}

	public void updateOrder(Order order) throws DataAccessException
	{
		this.getSqlMapClientTemplate().update("updateOrder", order);
	}
	
	/**内部测试函数*/
	public List<Order> page(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("page", page);
		map.put("range", range);
		return (List<Order>)getSqlMapClientTemplate().queryForList("getPageOrders", map);
	}
	
	/**
	 * 这个方法本来是private的, 后来由于一个中文测试的需要把它改成了public, 请不要到外部调用此函数.
	 * @param item
	 * @throws DataAccessException
	 */
	public void insertItem(Item item) throws DataAccessException
	{
		getSqlMapClientTemplate().insert("insertItem", item);
	}

	public List<Order> getOrdersByEventAndOdd(String eventid, String oddtype, int page, int range)
	{
		Map map = new HashMap();
		map.put("eventid", eventid);
		map.put("oddtype", oddtype);
		return (List<Order>)this.getSqlMapClientTemplate().queryForList("getOrdersByEventAndOdd", map , (page - 1) * range, range);
	}

	public List<Order> getTodayOrders( String yesterday, int page, int range)
	{
		Map map = new HashMap();
		map.put("yesterday", yesterday);
		return (List<Order>)this.getSqlMapClientTemplate().queryForList("getTodayOrders", null , (page - 1) * range, range);
	}

	public List<Order> getTodayOrdersByUser(String username,  String yesterday, int page, int range)
	{
		Map map = new HashMap();
		map.put("username", username);
		map.put("yesterday", yesterday);
		return (List<Order>)this.getSqlMapClientTemplate().queryForList("getTodayOrdersByUser", map , (page - 1) * range, range);
	}

	public List<Order> getOrders(Map map, int page, int range) throws DataAccessException
	{
		return (List<Order>)this.getSqlMapClientTemplate().queryForList("getOrder", map , (page - 1) * range, range);
	}

	public void deleteOrder(Order order)
	{
		getSqlMapClientTemplate().insert("deleteOrder", order);
	}
}